package com.youlu.campus.service.sesamekefu;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.OOrderInfo;
import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.entity.sesamekefu.SesameKeFuQuery;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.order.OrderInfoTipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SesameKeFuService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private OrderInfoTipService orderInfoTipService;


    public Page<OrderInfo> userOrderList(SesameKeFuQuery sesameKeFuQuery){
        String mobile = sesameKeFuQuery.getMobile();
        String name = sesameKeFuQuery.getName();
        String orderNo = sesameKeFuQuery.getOrderNo();
        String logisticsNo = sesameKeFuQuery.getLogisticsNo();
        if(StringUtils.isAllBlank(mobile,name,orderNo,logisticsNo)){
            throw new BusinessException("请输入收货人手机号、收货人姓名、订单号、物流单号其中任意一个");
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("status").ne("0"));
        if(StringUtils.isNotBlank(mobile)){
            query.addCriteria(Criteria.where("mobile").is(mobile));
        }
        if(StringUtils.isNotBlank(name)){
            query.addCriteria(Criteria.where("name").is(name));
        }
        if(StringUtils.isNotBlank(orderNo)){
            query.addCriteria(Criteria.where("orderNo").is(orderNo));
        }
        if (StringUtils.isNotBlank(logisticsNo)) {
            List<String> logisticsNoList = Arrays.asList(logisticsNo);
            query.addCriteria(Criteria.where("logisticsNos").in(logisticsNoList));
        }
        PageRequest pageRequest = PageRequest.of(sesameKeFuQuery.getPageNo() - 1, sesameKeFuQuery.getPageSize());
        long count = mongoTemplate.count(query, OrderInfo.class);
        if(count ==0){
            return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> count);
        }
        query.with(Sort.by(Sort.Direction.DESC,"payTime"));
        List<OrderInfo> orderInfoList = this.mongoTemplate.find(query.with(pageRequest), OrderInfo.class);
        orderInfoList.stream().forEach(orderInfo -> {
            if(StringUtils.isNotBlank(orderInfo.getActivityId())){
                ActivityInfo activityInfo = activityService.findOne(orderInfo.getActivityId());
                if(Objects.nonNull(activityInfo)){
                    orderInfo.setCustomerService(activityInfo.getCustomerService());
                    orderInfo.setRefundConfirmUrl(activityInfo.getRefundConfirmUrl());
                    orderInfo.setShowRefundConfirmUrl(activityInfo.getShowRefundConfirmUrl());
                }
            }
            Query subQuery = new Query();
            subQuery.addCriteria(Criteria.where("orderNo").is(orderInfo.getOrderNo()));
            List<OOrderInfo> subOrderList = this.mongoTemplate.find(subQuery, OOrderInfo.class);
            orderInfo.setOOrderInfos(subOrderList);
            //兼容老数据，如果是订单中是只有礼品的，前端不展示出姓名、学校
            if("ca".equals(orderInfo.getCategory())&&!CollectionUtils.isEmpty(orderInfo.getOOrderInfos())){
                //如果订单中无证书
                long caCount = orderInfo.getOOrderInfos().stream().filter(oOrderInfo -> !oOrderInfo.getCaGift()).count();
                if(caCount==0){
                    orderInfo.setCategory("gift");
                }
            }
            orderInfoTipService.setSubOrderTips(orderInfo);
        });
        return PageableExecutionUtils.getPage(orderInfoList, pageRequest, () -> count);
    }
}
